/* * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squareup.otto.sample.slice;

import com.squareup.otto.Subscribe;
import com.squareup.otto.sample.BusProvider;
import com.squareup.otto.sample.Message;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

public class PostSampleAbilitySlice extends AbilitySlice {
    private static String TAG = PostSampleAbilitySlice.class.getSimpleName();

    private static final int ID_TEXT = 100;
    private static final int ID_BUTTON = 200;
    private Button button;
    private Text text;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        LogUtil.info(TAG, "onStart() of Otto2");

        DirectionalLayout directionLayout = new DirectionalLayout(this);
        // Step 2 Set the layout size.
        directionLayout.setWidth(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        directionLayout.setHeight(ComponentContainer.LayoutConfig.MATCH_CONTENT);
        // Step 3 Set the layout attributes and ID (set the ID as required).
        directionLayout.setOrientation(Component.VERTICAL);

        // Step 4.1 Add layout attributes for the control.
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);

        text = new Text(this);
        text.setText("Hello World");
        text.setTextSize(50);
        text.setId(ID_TEXT);
        text.setLayoutConfig(layoutConfig);
        directionLayout.addComponent(text);

        // Step 4.2 Add Text to the layout.
        // Add a button in the similar way.
        button = new Button(this);
        button.setLayoutConfig(layoutConfig);
        button.setText("Click Here");
        button.setTextSize(60);
        button.setId(ID_BUTTON);
        ShapeElement background = new ShapeElement();
        background.setRgbColor(RgbColor.fromArgbInt(Color.GREEN.getValue()));
        button.setBackground(background);
        button.setPadding(10, 10, 10, 10);
        // Add a listener for the click event to the control.
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component Component) {
                LogUtil.info(TAG, "onClick() of Otto testcase2");

                BusProvider.getBus().register(this);
                BusProvider.getBus().post(new Message("From Main Thread Sub"));
            }
        });

        directionLayout.addComponent(button);
        super.setUIContent(directionLayout);
    }

    @Subscribe
    public void receiveMessage(Message msg) {
        text.setText(msg.getMsg() + " Name: " + Thread.currentThread().getName());
        text.setTextColor(Color.RED);
        text.invalidate();
    }

    @Override
    public void onActive() {
        BusProvider.getBus().register(this);
        super.onActive();
    }

    @Override
    public void onInactive() {
        BusProvider.getBus().unregister(this);
        super.onActive();
    }
}